﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Routing;
using TiTi.tea.Api.Attributes;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{

    [RoutePrefix("api/Login")]
    public class LoginController : ApiController
    {
        private LoginInfoBLL bll = new LoginInfoBLL();
        [Route("Query")]
        [HttpGet]
        public Result Query(string LoginName = "", int PageSize = 10, int PageIndex = 1)
        {
            int total = 0;
            var data = bll.Query(LoginName, out total, PageSize, PageIndex);
            return Result.Success(new { data, total });
        }
        [Route("get")]
        [HttpGet]
        public Result get(int id)
        {
            var data = bll.Get(id);
            return Result.Success(data);
        }
        [Route("Update")]
        [HttpPost]
        public Result Update(LoginInfo entity)
        {
            LoginInfo logininfo = bll.Get(entity.Id);
            logininfo.LoginName = entity.LoginName;
            logininfo.LoginPwd = entity.LoginPwd;
            logininfo.Status = entity.Status;
            if (bll.Update(logininfo))
            {
                return Result.Success(logininfo);
            }
            else
            {
                return Result.Error("修改失败");
            }
        }
        [Route("Add")]
        [HttpPost]
        public Result Add(LoginInfo entity)
        {

            entity.CreateTime = DateTime.Now;
            entity.IsDelete = 0;
            entity.Status = 0;
            if (bll.Add(entity))
            {
                return Result.Success(entity);
            }
            else
            {
                return Result.Error("添加失败");
            }
        }

        public Result Delete(int id)
        {
            return Result.Success(bll.Delete(id));
        }


        /// <summary>
        /// 顾客登录
        /// </summary>
        /// <param name="loginVM"></param>
        /// <returns></returns>
        [Route("login")]
        [HttpPost]
        public Result login(LoginVM loginVM)
        {
            LoginInfo login = bll.GetByLoginName(loginVM.LoginName);
            if (login == null) return Result.Error("账户不存在");
            if (login.Status != 1 || login.IsDelete != 0) return Result.Error("账户状态异常");
            if (login.LoginPwd != loginVM.LoginPwd) return Result.Error("密码有误");

            UserInfo user = bll.GetUserinfo(login.UserId ?? 0);

            AuthInfo auth = new AuthInfo()
            {
                UserId = user.Id,
                UserName = user.UserName
            };
            string jwt = JwtHelper.GenerateToken(auth);

            login.LoginCount++;
            login.LoginLastTime = DateTime.Now;
            bll.Update(login);

            return Result.Success(new { user, jwt });
        }

        [Route("UpPwd")]
        [HttpPost]
        [MyAuthorize]
        public Result UpPwd(PwdVM pwdVM) 
        {
            IHttpRouteData routeData = Request.GetRouteData();
            IDictionary<string, object> dict = routeData.Values;
            var auth = dict["auth"] as AuthInfo;
            var userId = auth.UserId;

            LoginInfo loginfo = bll.Get(userId);

            if (loginfo.LoginPwd == pwdVM.ypwd)
            {
                loginfo.LoginPwd = pwdVM.xpwd;
                if (bll.Update(loginfo))
                {
                    return Result.Success(loginfo);
                }
                else
                {
                    return Result.Error("修改失败");
                }
            }
            else {
                return Result.Error("密码不确认");

            }


           


        }

        [Route("GetAll")]
        [HttpGet]
        public Result GetAll()
        {
            return Result.Success(bll.Getall());
        }
    }

}